<template>
  <div class="app-container">
    <div id="lottie-container"></div>
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button
          type="primary"
          plain
          icon="el-icon-plus"
          size="mini"
          @click="handleAdd"
          v-hasPermi="['car:gift:add']"
        >新增</el-button>
      </el-col>
    </el-row>

    <el-table v-loading="loading" :data="dataList">
      <el-table-column label="通道名称" align="center" prop="channelName" width="160" :show-overflow-tooltip="true"/>
      <el-table-column label="通道类型" align="center" prop="channelType" width="160" :show-overflow-tooltip="true"/>
      <el-table-column label="最小金额" align="center" prop="minimumAmount" width="160" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ '$' + $yun.formatAmount(scope.row.minimumAmount) }}
        </template>
      </el-table-column>
      <el-table-column label="最大金额" align="center" prop="maximumAmount" width="160" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          {{ '$' + $yun.formatAmount(scope.row.maximumAmount) }}
        </template>
      </el-table-column>
      <el-table-column label="状态" align="center" prop="status" width="160" :show-overflow-tooltip="true">
        <template slot-scope="scope">
          <el-switch v-model="scope.row.state" active-color="#13ce66" inactive-color="#ff4949" :active-value="1"
                     :inactive-value="0" @change="statusChange(scope.row)" />
        </template>
      </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width" width="160">
        <template slot-scope="scope">
          <div>
            <el-button size="mini" type="text" icon="el-icon-edit" @click="handleUpdate(scope.row)">修改</el-button>
            <el-button size="mini" type="text" icon="el-icon-delete" @click="handleDelete(scope.row)">删除</el-button>
          </div>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改直播礼物对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="通道名称" prop="channelId">
          <el-select class="modal-input" v-model="form.channelId">
            <el-option class="modal-input" v-for="(item, index) in passageList" :key="index" :label="item.channelName"
                       :value="item.countryId"
            ></el-option>
          </el-select>
        </el-form-item>
        <el-form-item label="通道类型" prop="channelType">
          <el-input v-model="form.channelType" placeholder="请输入通道类型" />
        </el-form-item>
        <el-form-item label="最小金额" prop="minimumAmount">
          <el-input type="number" :min="0" v-model="form.minimumAmount" placeholder="请输入最小金额" />
        </el-form-item>
        <el-form-item label="最大金额" prop="maximumAmount">
          <el-input type="number" :min="0" v-model="form.maximumAmount" placeholder="请输入最大金额" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm(form)">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import OssImagePreview from '@/components/ImagePreview/oss-image-preview.vue'

import lottie from 'lottie-web';
import lottieConfig from 'lottie-web/build/player/lottie_svg';
import { list, del, add, edit } from "@/api/payment/payment";
import {channelList} from "@/api/system/passage";
import {formatAmount} from "@/utils/yun";
import Template from "@/views/setting/template/index.vue";


export default {
  name: "Gift",
  components: {Template, OssImagePreview },
  dicts:['gift_status'],
  data() {
    return {
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 直播礼物表格数据
      dataList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
      },
      // 表单参数
      form: {},
      // 表单校验
      rules: {
        channelId: [
          { required: true, message: "通道名称不能为空", trigger: "change" }
        ],
        channelType: [
          { required: true, message: "通道类型不能为空", trigger: "blur" }
        ],
        minimumAmount: [
          { required: true, message: "最小金额不能为空", trigger: "blur" }
        ],
        maximumAmount: [
          { required: true, message: "最大金额不能为空", trigger: "blur" }
        ]
      },
      passageList:[],
    };
  },
  created() {
    this.getList();
    // 查询通道
    channelList({}).then(response => {
      this.passageList = response.data;
      console.log('this.passageList',this.passageList)
    });
  },
  // 用来展示svga
  mounted() {
    const container = document.getElementById('lottie-container');
    const animation = lottie.loadAnimation({
      container: container,
      path: 'https://live-mall-1319900789.cos.ap-singapore.myqcloud.com/sys_other/2023-08-18/16923431586688265.svga', // 替换为你的SVGA文件路径
      renderer: 'svg',
      loop: true,
      autoplay: true,
      ...lottieConfig // 引入配置
    });
  },
  methods: {
    formatAmount,
    /** 查询直播礼物列表 */
    getList() {
      this.loading = true;
      list(this.queryParams).then(response => {
        this.dataList = response.data.records;
        this.total = response.data.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
      this.$refs.form.clearValidate();
    },
    // 表单重置
    reset() {
      this.form = {};
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加支付平台";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      this.form = JSON.parse(JSON.stringify(row))
      this.form.channelId += '';
      this.open = true;
      this.title = "修改支付平台";
    },
    /** 提交按钮 */
    submitForm(row) {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            let obj = {
              id:row.id,
              channelType:row.channelType,
              channelId:row.channelId,
              minimumAmount:row.minimumAmount,
              maximumAmount:row.maximumAmount,
            }
            edit(obj).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            add(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      this.$modal.confirm('是否确认删除该数据？').then(function() {
        return del(row.id);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 修改状态 */
    statusChange(row) {
      edit(row).then(response => {
        this.$modal.msgSuccess("修改成功");
        this.getList();
      });
    },
  }
};
</script>
